package xhwl.insurance.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.CardDetailsAdapter;
import xhwl.insurance.adapter.CardNewDetailsAdapter;
import xhwl.insurance.http.api.CardDetailApi;
import xhwl.insurance.http.bean.resp.CardDetailResq;
import xhwl.insurance.http.bean.resp2.HttpResp;
import xhwl.insurance.util.XHLog;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

import static xhwl.insurance.R.id.pullToRefreshListView;

/**
 * Created by chengang on 2017/7/28.
 */

public class CardDetailsActivity extends BaseActivity {

    private EditText edit_search;
    private ImageView search_but;
    private ListView mListView;
    private LinearLayout emptyView;
    private CardNewDetailsAdapter adapter;
    private ArrayList<CardDetailResq> mlistVo = new ArrayList<CardDetailResq>();
    private String sale_type = "1";
    private String days;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_details);
        initView();
    }

    @Override
    public void initViews() {
        initTitleViews();
        edit_search = (EditText) findViewById(R.id.edit_search);
        search_but = (ImageView) findViewById(R.id.search_but);
        emptyView = (LinearLayout) findViewById(R.id.emptyView);
        mListView = (ListView) findViewById(pullToRefreshListView);
//        adapter = new CardDetailsAdapter(CardDetailsActivity.this, mlistVo);
//        mListView.setAdapter(adapter);

        //新版售卡详情页
          adapter = new CardNewDetailsAdapter(CardDetailsActivity.this, mlistVo);
        mListView.setAdapter(adapter);
    }


    @Override
    protected void setUpTitleView() {
        titleTextView.setText("HMO售卡详情");
        super.setUpTitleView();
    }

    @Override
    public void initListener() {
        search_but.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                mlistVo.clear();
                String editSearch = edit_search.getText().toString().trim();
                requestList(editSearch);
            }
        });
    }

    @Override
    public void loadData() {
        Intent intent = getIntent();
        sale_type = intent.getStringExtra("sale_type");
        days = intent.getStringExtra("days");

        requestList("");
    }


    private void requestList(String person_name) {
        String link_mobile = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.LINK_MOBILE, "");
        CardDetailApi api = new CardDetailApi(link_mobile, sale_type, person_name, days);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<ArrayList<CardDetailResq>>>() {

            @Override
            public void onNext(HttpResp<ArrayList<CardDetailResq>> resp) {
                if (resp != null) {
                    mListView.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                    if (resp.isOk()) {
                        if (resp.data != null && resp.data.size() > 0) {
                            mlistVo.addAll(resp.data);
                            adapter.notifyDataSetChanged();
                        }

                    } else {
                        ToastUtil.showToast(CardDetailsActivity.this, resp.message);
                    }
                } else {
                    mListView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                mListView.setVisibility(View.GONE);
                emptyView.setVisibility(View.VISIBLE);
            }
        });
    }
            private void initView() {
                TextWatcher textWatcher = new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (s.length() == 0) {
                            mlistVo.clear();
                            requestList("");
                        } else {

                        }
                    }
                };
                edit_search.addTextChangedListener(textWatcher);

            }

        }

